using System;
using System.Collections.Generic;
using System.Text;
using ET = ErlyThreads;

namespace Testbed
{
    class Program
    {
        public static readonly int SUCCESS = 0;
        public static readonly int FAILURE = 1;

        static void Main(string[] args)
        {
            ET.Tests.WaitForMessagesTest t = new ErlyThreads.Tests.WaitForMessagesTest();
        }

        static int MainBackup(string[] args)
        {
            int numberOfTrees = 5;
            int depth = 3;

            //{
            //    Node theSameTree = Node.BuildRandomContentsTree(depth);
            //    Node[] tenEqualTrees = new Node[numberOfTrees];
            //    for (int i = 0 ; i < tenEqualTrees.Length ; i++)
            //    {
            //        tenEqualTrees[i] = theSameTree;
            //    }

            //    if (!TreeCompare.AreEqual(tenEqualTrees))
            //    {
            //        throw new ApplicationException(
            //            "The checker did not work");
            //    }
            //}

            {
                Node[] tenDifferentTrees = new Node[numberOfTrees];
                for (int i = 0 ; i < tenDifferentTrees.Length ; i++)
                {
                    tenDifferentTrees[i] = Node.BuildRandomContentsTree(depth);
                }

                if (TreeCompare.AreEqual(tenDifferentTrees))
                {
                    throw new ApplicationException(
                        "The checker did not work.");
                }
            }

            return SUCCESS;
        }

        public static string NewID()
        {
            return Guid.NewGuid().ToString();
        }

  
    }

    class NSnakes
    {
        public static int Run()
        {
            NSnakes snakes = new NSnakes();
            int index = 0;

            while (!Console.KeyAvailable)
            {
                snakes.SetSnakeValue(
                    index = ( new Random() ).Next(20),
                    snakes.GenSnakeValue(index) + 1);
                snakes.Update();
                System.Threading.Thread.Sleep(150);
            }

            return Program.SUCCESS;
        }

        IDictionary<int, int> mSnakesCounter =
            new Dictionary<int, int>();

        public NSnakes()
        {

        }

        public void SetSnakeValue(int pSnakeNumber, int pValue)
        {
            mSnakesCounter[pSnakeNumber] = pValue;
        }

        public int GenSnakeValue(int pSnakeNumber)
        {
            if (mSnakesCounter.Keys.Contains(pSnakeNumber))
            {
                return mSnakesCounter[pSnakeNumber];
            }
            else
            {
                return 0;
            }
        }

        int mLastMax = int.MinValue, mLastMin = int.MaxValue;

        public void Update()
        {
            int
                min = int.MaxValue,
                max = int.MinValue;

            foreach (int i in mSnakesCounter.Keys)
            {
                min = Math.Min(i, min);
                max = Math.Max(i, max);
            }

            if (min != mLastMin ||
                max != mLastMax)
            {
                Console.Clear();
                mLastMax = max;
                mLastMin = min;
            }

            Console.SetCursorPosition(0, 0);
            Console.WriteLine("Press any key to quit...\n");

            for (int i = min ; i <= max ; i++)
            {
                int nr = 0;
                if (mSnakesCounter.Keys.Contains(i))
                {
                    nr = mSnakesCounter[i];
                }

                Console.WriteLine("#{0}: {1}",
                    i,
                    Microsoft.VisualBasic.Strings.StrDup(nr, "*"));
            }

        }
    }
}
